<template>
  <div class="departments">
    <el-card>
      <template #header>
        <div>组织架构</div>
      </template>
      <div class="tree">
        <Treeitem :item="topInfo" />
        <hr>
        <el-tree :data="list" :props="{ children: 'children' }">
          <template v-slot="{ data }">
            <Treeitem :item="data" style="width: 100%" @getData="getData" />
          </template>
        </el-tree>
      </div>
    </el-card>
    <Departments ref="dialogItem" :initlist="initlist" @getData="getData" />
  </div>
</template>

<script>
import Treeitem from './components/tree-item.vue'
import { companyDepartment } from '@/api/departments'
import Departments from './components/departments.vue'
export default {
  components: {
    Treeitem,
    Departments
  },
  data() {
    return {
      topInfo: {
        name: '腾讯控股',
        manager: '负责人',
        isTop: true,
        id: ''
      },
      list: [],
      initlist: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await companyDepartment()
      this.topInfo.name = res.data.companyName
      this.initlist = res.data.depts
      this.list = this.changeData(res.data.depts, '')
      console.log('部门列表:', res)
    },
    changeData(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.departments {
  padding: 30px;
  .tree {
    width: 80%;
    margin: 0 auto;
  }
}
</style>
